package com.bookshopsystem.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.bookshopsystem.dao.DaoFactory;
import com.bookshopsystem.mod.Role;
import com.bookshopsystem.mod.States;
import com.bookshopsystem.mod.User;

/**
 * Servlet implementation class Admin_UserServlet
 */
@WebServlet("/Admin_UserServlet")
public class Admin_UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Admin_UserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String type=request.getParameter("type");
		String data;
		JSONArray jsonArray;
		JSONObject jsonObject;
		switch (type) {
		case "userCount":
			data=request.getParameter("data");
			try {
				int len=DaoFactory.getUserDaoInstance().usersCount(Integer.parseInt(data));
				if (len==0) {
					len=1;
				}
				response.getWriter().print("[{'userPageNum':'"+len+"'}]");
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				response.getWriter().print("[{'userPageNum':'1'}]");
			}
			break;
		case "userInfo":
			data=request.getParameter("data");
			jsonObject=new JSONObject().fromObject(data);
			try {
				List<Object> list=new ArrayList<Object>();
				List<Role> roles=DaoFactory.getUserDaoInstance().userRoles();
				list.add(roles);
				List<States> states=DaoFactory.getUserDaoInstance().userStates();
				list.add(states);
				List<User> users=DaoFactory.getUserDaoInstance().findUserAll(
						Integer.parseInt(jsonObject.getString("page")),
						Integer.parseInt(jsonObject.getString("pageCount")));
				list.add(users);
				jsonArray=new JSONArray().fromObject(list);
				response.getWriter().print(jsonArray);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		case "deleteUser":
			data=request.getParameter("data");
			try {
				if (DaoFactory.getUserDaoInstance().deleteUser(Integer.parseInt(data))) {
					response.getWriter().print("[{'deleteUser':'success'}]");
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		case "updateUser":
			data=request.getParameter("data");
			jsonObject=new JSONObject().fromObject(data);
			User user=(User) JSONObject.toBean(jsonObject, User.class);
			try {
				if (DaoFactory.getUserDaoInstance().alterUser(user)) {
					response.getWriter().print("[{'updateUser':'success'}]");
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		case "login":
			data=request.getParameter("data");
			jsonObject=new JSONObject().fromObject(data);
			String loginName=jsonObject.getString("loginId");
			String loginPwd=jsonObject.getString("loginPwd");
			try {
				if (DaoFactory.getUserDaoInstance().isValib(loginName)&&
					DaoFactory.getUserDaoInstance().getUserRoleId(loginName)==3&&
					DaoFactory.getUserDaoInstance().login(loginName,loginPwd)) {
					request.getSession().setAttribute("admin_login", DaoFactory.getUserDaoInstance().findUserById(loginName));
						response.getWriter().print(JSONObject.fromObject("{'admin_login':'pass'}"));
				}else {
					response.getWriter().print(JSONObject.fromObject("{'admin_login':'false'}"));		
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				response.getWriter().print(JSONObject.fromObject("{'admin_login':'false'}"));
			}
			break;
		case "isLogin":
			if (request.getSession().getAttribute("admin_login")!=null) {
				User u=(User) request.getSession().getAttribute("admin_login");
				response.getWriter().print(JSONObject.fromObject("{'admin_login':'pass','name':'"+u.getUserName()+"'}"));
			}else {
				response.getWriter().print(JSONObject.fromObject("{'admin_login':'false'}"));
			}
			break;
		default:
			break;
		}
	}

}
